package com.mhz.cae.hud;

import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;

import com.mhz.cae.events.AddItemEvent;
import com.mhz.cae.events.EventManager;
import com.mhz.cae.objects.ItemType;

/**
 * Represents an item box into the inventory
 */
public class ItemBox extends Rectangle {

	private static final float BOX_WIDTH = 140;
	private static final float BOX_HEIGHT = 50;
	private static final float DEFAULT_X = 5;

	private ChangeableText mBoxText = null;
	private final String mBoxLabel;

	public ItemBox(String label, Font font, float pY) {
		super(DEFAULT_X, pY, BOX_WIDTH, BOX_HEIGHT);
		mBoxLabel = label;
		mBoxText = new ChangeableText(5, 5, font, mBoxLabel + " : ", (mBoxLabel + " : XX").length());
		this.setColor(0.1f, 0.2f, 0.3f);
		this.attachChild(mBoxText);
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
		switch (pSceneTouchEvent.getAction()) {
		case TouchEvent.ACTION_DOWN:
			// Get item from inventory and add it to the scene
			ItemType itemSelected = (ItemType) this.getUserData();
			if (itemSelected != null && itemSelected.isAvailable()) {
				itemSelected.disposeItem();
				setNumberItem(itemSelected.getCurrentNumber());
				EventManager.getInstance().pushEvent(new AddItemEvent(itemSelected));
			}
		}
		return true;
	}

	public void setNumberItem(int number) {
		mBoxText.setText(mBoxLabel + " : " + number);
	}

}
